package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("sessions")
public class SessionController {
    @Autowired
    private IUserService userService;

    /**
     * 登陆验证
     * @param email
     * @param password
     * @return JSON
     */
    @PostMapping
    public JSONResult login(@RequestParam String email, @RequestParam String password){
        JSONResult result = new JSONResult();
        try {
            User user = userService.login(email, password);
            // 用户存在,将用户信息存放到session
            UserContext.setCurrentUser(user);
            result.setData(user);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    @DeleteMapping
    public JSONResult logout(HttpSession session){
        session.invalidate();
      return new JSONResult();
    }
}
